
package com.jf.cloud.auth.controller;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.api.auth.constant.LoginType;
import com.jf.cloud.api.auth.constant.SysTypeEnum;
import com.jf.cloud.api.auth.vo.TokenInfoVO;
import com.jf.cloud.api.delivery.feign.StationFeignClient;
import com.jf.cloud.api.delivery.vo.StationVO;
import com.jf.cloud.api.rbac.dto.ClearUserPermissionsCacheDTO;
import com.jf.cloud.api.rbac.feign.PermissionFeignClient;
import com.jf.cloud.auth.dto.AuthenticationDTO;
import com.jf.cloud.auth.manager.PasswordManager;
import com.jf.cloud.auth.manager.TokenStore;
import com.jf.cloud.auth.service.AuthAccountService;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/6/30
 */
@RestController
@Tag( name = "自提点登录")
public class StationLoginController {

    @Autowired
    private TokenStore tokenStore;
    @Autowired
    private PasswordManager passwordManager;
    @Autowired
    private AuthAccountService authAccountService;
    @Autowired
    private PermissionFeignClient permissionFeignClient;
    @Autowired
    private StationFeignClient stationFeignClient;


    @PostMapping("/ua/station_login")
    @Operation(summary = "自提点账号密码(用于前端登录)", description = "通过账号/手机号/用户名密码登录")
    public ServerResponseEntity<TokenInfoVO> login(@Valid @RequestBody AuthenticationDTO authenticationDTO) {
        authenticationDTO.setSysType(SysTypeEnum.STATION.value());
        // 账号密码校验
        ServerResponseEntity<UserInfoInTokenBO> userInfoInTokenResponse = authAccountService.getUserInfoInTokenByInputUserNameAndPassword(
                authenticationDTO.getUserName(),
                authenticationDTO.getPassWord(),
                authenticationDTO.getSysType(),
                LoginType.PASSWORD.value()
        );

        if (!userInfoInTokenResponse.isSuccess()) {
            return ServerResponseEntity.transform(userInfoInTokenResponse);
        }

        UserInfoInTokenBO data = userInfoInTokenResponse.getData();
        // 校验门店状态是否正常
        StationVO stationVO = stationFeignClient.getStationByInside(data.getTenantId()).getData();
        if (stationVO == null) {
            throw new LuckException("门店不存在");
        }
        if (!Objects.equals(stationVO.getStatus(), 1)) {
            throw new LuckException("该门店已关闭，请联系管理员");
        }
        // 清理权限缓存
        ClearUserPermissionsCacheDTO clearUserPermissionsCacheDTO = new ClearUserPermissionsCacheDTO();
        clearUserPermissionsCacheDTO.setSysType(data.getSysType());
        clearUserPermissionsCacheDTO.setUserId(data.getUserId());
        ServerResponseEntity<Void> clearResponseEntity = permissionFeignClient.clearUserPermissionsCache(clearUserPermissionsCacheDTO);
        if (!clearResponseEntity.isSuccess()) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        // token存储返回
        return ServerResponseEntity.success(tokenStore.storeAndGetVo(data));
    }
}
